package com.atwulidun.queue;

import java.util.Scanner;

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue queue = new CircleArrayQueue(4);// 实际有效长度为3
        char key;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    try {
                        queue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    System.out.print("程序退出，谢谢使用!");
                    loop = false;
                    break;
                case 'a':
                    System.out.println("请输入要填加到队列的数据：");
                    int data = scanner.nextInt();
                    queue.addQueue(data);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.println("取出的数据为：" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.println("头部数据为：" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }
    }
}

class CircleArrayQueue {
    private int maxSize;
    private int front;// 指向队列的第一个元素
    private int rear;// 指向队列最后一个元素的下一位
    private int[] arr;

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
//        front = 0;
//        rear = 0;
        arr = new int[maxSize];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (rear + 1 ) % maxSize == front;// 以rear的值为maxSize - 1，front的值为0为例
    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满，不能添加！");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;// rear指针后移，因为rear的值不对外暴露，因此只需在0~3之间变化即可
    }

    // 获取队列的头数据，此时front的值增加1
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取数据！");
        }
        int temp = arr[front];// 取出元素后要把该位置置0
        arr[front] = 0;
        front = (front + 1) % maxSize;// front指针后移，因为front的值不对外暴露，因此只需在0~3之间变化即可
        return temp;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，不能显示数据！");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d] = %d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    private int size() {
        return (rear + maxSize - front) % maxSize;// 队列中有效数据的个数(以rear为0，front为1为例)
    }

    // 查看队列的头数据，此时front的值不变
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取数据");
        }
        return arr[front];// 注意不能让front自增
    }
}


//class CircleArrayQueue {
//    private int[] queue;
//    private int maxSize;
//    private int front;
//    private int rear;
//
//    public CircleArrayQueue(int maxSize) {
//        this.maxSize = maxSize;
//        queue = new int[maxSize];
//    }
//
//    public boolean isEmpty() {
//        return front == rear;
//    }
//
//    public boolean isFull() {
//        return (rear + 1) % maxSize == front;
//    }
//
//    public void addQueue(int num) {
//        if (isFull()) {
//            System.out.println("the queue is full!");
//            return;
//        }
//        queue[rear] = num;
//        rear = (rear + 1) % maxSize;
//    }
//
//    public int getQueue() {
//        if (isEmpty()) {
//            throw new RuntimeException("the queue is empty!");
//        }
//        int temp = queue[front];
//        queue[front] = 0;
//        front = (front + 1) % maxSize;
//        return temp;
//    }
//
//    public int headQueue() {
//        if (isEmpty()) {
//            throw new RuntimeException("the queue is empty!");
//        }
//        return queue[front];
//    }
//
//    public void showQueue() {
//        if (isEmpty()) {
//            throw new RuntimeException("the queue is empty!");
//        }
//        for (int i = front; i < front + size(); i++) {
//            System.out.printf("arr[%d]=%d\n", i % maxSize, queue[i % maxSize]);
//        }
//    }
//
//    private int size() {
//        return (rear + maxSize - front) % maxSize;
//    }
//}